package com.xdf.ecommerce.service.communication;

import feign.Feign;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author chanchaw
 * @create 2025-10-29 14:31
 */
@Configuration
@ConditionalOnClass(Feign.class)// 依赖 Feign.class 的实体类，即在其初始化之后再初始化本配置类
@AutoConfigureBefore(FeignAutoConfiguration.class) // 在 FeignAutoConfiguration.class 之前先初始化本配置类
public class FeignOkHttpConfig {
    @Bean
    public OkHttpClient okHttpClient(){
        return new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS) // 连接超时
                .readTimeout(5,TimeUnit.SECONDS) // 读取超时
                .writeTimeout(5,TimeUnit.SECONDS) // 写入超时
                .retryOnConnectionFailure(true) // 自动重连
                // 配置连接池中的最大空闲线程个数为10，并保持5分钟，实现重复使用线程的目标
                .connectionPool(new ConnectionPool(10,5L,TimeUnit.MINUTES))
                .build();
    }
}
